<?php
//require_once("../database_common.php");
require_once(dirname(__FILE__)."/../common/constants.php");
require_once(DB_SESSION_DIR_PATH."/database_common.php");
require_once(DB_SESSION_DIR_PATH."/db_operator.php");
require_once(COMMON_DIR_PATH."/common_util.php");
/*
 * Created on
 * Date: 2007-12-3
 * Author: William Yang
 */

class Session_Controller {
	private $sess_save_path;
	private $sess_session_name;
	
	function open($save_path, $session_name) {
		$this->sess_save_path 		= $save_path;
		$this->sess_session_name	= $session_name;
		return (true);
	}

	function close() {
		return (true);
	}
	
	function read($id) {
		$sess_file = SESSION_FILES_DIR_PATH."sess_{$id}";
		if ($fp = @fopen($sess_file, "r")) {
			$sess_data = @fread($fp, filesize($sess_file));
			return ($sess_data);
		} else {
			return (""); //   Must   return   ""   here.  
		}
	
	}
	
	function write($id, $sess_data) {
		$sess_file = SESSION_FILES_DIR_PATH."sess_{$id}";
		if ($fp = fopen($sess_file, "w")) {
			return (fwrite($fp, $sess_data));
		} else {
			return (false);
		}
	
	}
	
	function destroy($id) {
		/*
		 * Set the corresponding session_id to NULL in database.
		 */
		$updated_array = array();
		$updated_array['session_id'] = NULL;
		$condition = new SqlCondition('session_id', SqlCondition::EQUAL, $id);
		$c = new SqlCondList();
		$c->add_condition($condition);			
		$opera = new DatabaseOperator();
		$opera->config(CONFIGURATION_DIR_PATH."/user_table.xml");
		$opera->update($updated_array, $c);
		
		$sess_file = "$this->sess_save_path/sess_$id";
		return @unlink($sess_file);
	}
	
	/*********************************************  
	  *   WARNING   -   You   will   need   to   implement   some   *  
	  *   sort   of   garbage   collection   routine   here.     *  
	  *********************************************/
	function gc($maxlifetime) {
		$all = opendir(SESSION_FILES_DIR_PATH);
		while ($file = readdir($all)) {
			$thisfile = SESSION_FILES_DIR_PATH.$file;
			if (is_file($thisfile) && (!StringUtils::is_end_with($thisfile, ".php", TRUE))) {
				$last_accessed_ts = fileatime($thisfile);
				if ($last_accessed_ts != FALSE && (time() - $last_accessed_ts) > $maxlifetime) {
					@unlink($thisfile);
					$this->expire_user_session_status(trim($file, "sess_"));
				}
			}
		}
		closedir($all);
		unset ($all);
		return true;
	}
	
	public function expire_user_session_status($sess_id) {
		$updated_array = array();
		$updated_array['session_id'] = NULL;
		$condition = new SqlCondition('session_id', SqlCondition::EQUAL, $sess_id);
		$c = new SqlCondList();
		$c->add_condition($condition);			
		$opera = new DatabaseOperator();
		$opera->config(CONFIGURATION_DIR_PATH."/user_table.xml");
		$opera->update($updated_array, $c);
	}

	public function __construct() {
		$this->sess_save_path = SESSION_FILES_DIR_PATH;
		$this->sess_session_name = "Fuzsession";
		
		session_set_save_handler(
			array(&$this, "open"),
			array(&$this, "close"),
			array(&$this, "read"),
			array(&$this, "write"),
			array(&$this, "destroy"),
			array(&$this, "gc"));
		session_save_path(SESSION_FILES_DIR_PATH);
	}

	public function start_session() {
		session_start();
	}
	
	public function destroy_session() {
		session_destroy();
	}
}

?>
